package com.gxwy.upanyonghu.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gxwy.upanyonghu.form.UpanYongHu;

@Repository
public class UpanYongHuImpl implements UpanYongHuDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public UpanYongHu findUpanYongHu(String username) {
		String hql="from UpanYongHu u where u.username=?";
		return (UpanYongHu) sessionFactory.getCurrentSession().createQuery(hql).setString(0,username).uniqueResult();
	}

	public void updateDengLuShiJian(UpanYongHu user) {
		sessionFactory.getCurrentSession().update(user);
	}

	public void updatePassword(UpanYongHu uyh) {
			sessionFactory.getCurrentSession().update(uyh);
			
	}

	public List<UpanYongHu> findAll() {

		return sessionFactory.getCurrentSession().createQuery("from UpanYongHu").list();

	}
	public void Update(UpanYongHu contact,Integer id) {

			String hql="Update UpanYongHu AS m SET m.realname=?,m.idcard=?,m.job=?,m.password=?,m.phone=?,m.telephone=?,m.username=?,m.usetype=?,m.zipcode=?,m.adress=?,m.dongjie=?,m.email=?,m.gender=? where m.id=?";

		
		Query query=(Query) sessionFactory.getCurrentSession().createQuery(hql);
		((org.hibernate.Query) query).setString(0,contact.getRealname());
		query.setString(1, contact.getIdcard_id());
		query.setString(2, contact.getJob());
		query.setString(3,contact.getPassword());
		query.setString(4, contact.getPhone());
		query.setString(5,contact.getTelephone());
		query.setString(6,contact.getUsername());
		query.setString(7, contact.getUsertype());
		query.setString(8, contact.getZipcode());
		query.setString(9, contact.getAddress());
		query.setString(10,contact.getDongjie());
		query.setString(11, contact.getEmail());
		query.setString(12,contact.getGender());
		query.setInteger(13, id);
		query.executeUpdate();
	}

	public void save(UpanYongHu contact) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(contact);

	}

	public void Delete(Integer id) {
		// TODO Auto-generated method stub
		UpanYongHu u = (UpanYongHu) sessionFactory.getCurrentSession().load(
				UpanYongHu.class, id);
		if (null != u) {
			sessionFactory.getCurrentSession().delete(u);
		}
	}

}
